package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import domains.Contingent;
import mappers.ContingentMapper;

public class RenewContingentGUI {
	private JFrame renewContingentFrame; 
	private JTextField memberIdTF, startDateTF, endDateTF;
	private JLabel memberIdLbl, fNameLbl, lNameLbl, startDateLbl, endDateLbl;
	private JPanel layoutPanel, enterPressedPanel, btnPanel;
	private String fName, lName;
	private JButton addBtn, cancelBtn;
	private int id;
	
	public RenewContingentGUI() {
		layoutPanel = new JPanel();
		layoutPanel.setLayout(new BoxLayout(layoutPanel, BoxLayout.Y_AXIS));
		
		enterPressedPanel = new JPanel();
		enterPressedPanel.setLayout(new BoxLayout(enterPressedPanel, BoxLayout.Y_AXIS));
		
		btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		
		memberIdLbl = new JLabel("Indtast medlemsID:");	
		memberIdTF = new JTextField();
		memberIdTF.setMaximumSize(new Dimension(100, 20));
		
		fNameLbl = new JLabel();
		lNameLbl = new JLabel();
		startDateLbl = new JLabel("Startdato: (eks.: 01-01-2013)");
		startDateTF = new JTextField();
		startDateTF.setMaximumSize(new Dimension(250, 20));
		endDateLbl = new JLabel("Slutdato: (eks.: 01-02-2013)");
		endDateTF = new JTextField();
		endDateTF.setMaximumSize(new Dimension(250, 20));
		
		InputMap im = memberIdTF.getInputMap(JTextField.WHEN_IN_FOCUSED_WINDOW);
		ActionMap am = memberIdTF.getActionMap();
		im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "ENTER");
		am.put("ENTER", new AbstractAction() {

			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent arg0) {
				ContingentMapper cMapper = new ContingentMapper();
		
				id = Integer.parseInt(memberIdTF.getText());
				ArrayList<Contingent> contingent = cMapper.renewContingentInfo(id);
				enterPressedPanel.removeAll();
				for (int i = 0; i < contingent.size(); i++) {
					fName = contingent.get(i).getfName();
					lName = contingent.get(i).getlName();
				}
				
				fNameLbl.setText("Fornavn: " + fName);
				lNameLbl.setText("Efternavn: " + lName);
				
				enterPressedPanel.add(Box.createRigidArea(new Dimension(0, 5)));
				enterPressedPanel.add(fNameLbl);
				enterPressedPanel.add(lNameLbl);
				enterPressedPanel.add(Box.createRigidArea(new Dimension(0, 5)));
				enterPressedPanel.add(new JSeparator(JSeparator.HORIZONTAL));
				enterPressedPanel.add(Box.createRigidArea(new Dimension(0, 5)));
				enterPressedPanel.add(startDateLbl);
				enterPressedPanel.add(startDateTF);
				enterPressedPanel.add(Box.createRigidArea(new Dimension(0, 5)));
				enterPressedPanel.add(endDateLbl);
				enterPressedPanel.add(endDateTF);
				layoutPanel.add(enterPressedPanel);
				enterPressedPanel.revalidate();
				enterPressedPanel.validate();
				enterPressedPanel.repaint();
			}
		});
		
		layoutPanel.add(memberIdLbl);
		layoutPanel.add(Box.createRigidArea(new Dimension(0, 5)));
		layoutPanel.add(memberIdTF);
		
		addBtn = new JButton("Forny");
		addBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ContingentMapper cMapper = new ContingentMapper();
				DateTime startDate = parseDateTime(startDateTF.getText());
				DateTime endDate = parseDateTime(endDateTF.getText());
				Contingent c = new Contingent(id, startDate, endDate);
				cMapper.addContingent(c);
				JOptionPane.showMessageDialog(null, "Kontingent er fornyet!");
				renewContingentFrame.setVisible(false);
			}		
		});
		cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				renewContingentFrame.setVisible(false);
			}		
		});

		btnPanel.add(addBtn);
		btnPanel.add(cancelBtn);
	}
	
	public void showGUI() {
		renewContingentFrame = new JFrame("Forny Kontingent");
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
		int w = renewContingentFrame.getSize().width;
		int h = renewContingentFrame.getSize().height;
		int x = (dim.width-w)/4;
		int y = (dim.height-h)/4;
		renewContingentFrame.setLocation(x, y);
		renewContingentFrame.setLayout(new BorderLayout());
		renewContingentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		renewContingentFrame.setSize(400, 400);
		renewContingentFrame.add(layoutPanel, BorderLayout.NORTH);
		renewContingentFrame.add(btnPanel, BorderLayout.SOUTH);
		renewContingentFrame.setVisible(true);
	}
	
	private static DateTime parseDateTime(String input) {
		String pattern = "dd-MM-yy";
		DateTime dateTime = DateTime.parse(input, DateTimeFormat.forPattern(pattern));		
		return dateTime;
	}
	
}
